package xyz.yuzegod.megawalls.classes.mythic.phoenix;

import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.Sound;
import org.bukkit.block.Block;
import org.bukkit.entity.Player;
import org.bukkit.plugin.Plugin;
import org.bukkit.potion.PotionEffect;
import org.bukkit.potion.PotionEffectType;
import xyz.yuzegod.megawalls.MegaWalls;
import xyz.yuzegod.megawalls.classes.Classes;
import xyz.yuzegod.megawalls.classes.Skill;
import xyz.yuzegod.megawalls.game.GamePlayer;
import xyz.yuzegod.megawalls.stats.KitStatsContainer;
import xyz.yuzegod.megawalls.util.LocationUtils;
import xyz.yuzegod.megawalls.util.ParticleEffect;
import xyz.yuzegod.megawalls.util.PlayerUtils;

import java.util.*;

public class MainSkill extends Skill
{
    private static final Set<Material> set;
    
    public MainSkill(final Classes classes) {
        super("阳炎射线", classes);
    }
    
    @Override
    public int maxedLevel() {
        return 5;
    }
    
    @Override
    public double getAttribute(final int level) {
        switch (level) {
            case 1: {
                return 2.2;
            }
            case 2: {
                return 2.4;
            }
            case 3: {
                return 2.6;
            }
            case 4: {
                return 2.8;
            }
            case 5: {
                return 3.0;
            }
            default: {
                return 2.2;
            }
        }
    }
    
    @Override
    public List<String> getInfo(final int level) {
        final List<String> lore = new ArrayList<String>();
        if (level == 1) {
            lore.add(" §8▪ §7左键你的弓来发射一道阳炎射线");
            lore.add("   §7在阳炎射线落点附近的队友将恢复§a" + this.getAttribute(level) + "§7生命值并获得速度I,持续10秒");
            lore.add("   §7消耗28能量");
            lore.add("   §7每次攻击获得能量(近战):6");
            lore.add("   §7每次攻击获得能量(弓箭):12");
            return lore;
        }
        lore.add(" §8▪ §7在阳炎射线落点附近的队友将恢复§8" + this.getAttribute(level - 1) + " ➜");
        lore.add("   §a" + this.getAttribute(level) + "§7生命值并获得速度I,持续10秒");
        return lore;
    }
    
    @Override
    public void upgrade(final GamePlayer gamePlayer) {
        final KitStatsContainer kitStats = gamePlayer.getPlayerStats().getKitStats(this.getClasses());
        kitStats.addSkillLevel();
    }
    
    @Override
    public int getPlayerLevel(final GamePlayer gamePlayer) {
        return gamePlayer.getPlayerStats().getKitStats(this.getClasses()).getSkillLevel();
    }
    
    @Override
    public String getSkillTip(final GamePlayer gamePlayer) {
        return this.getClasses().getNameColor() + this.getName() + " §e§l" + gamePlayer.getEnergy() / 25;
    }
    
    @Override
    public boolean use(final GamePlayer gamePlayer, final KitStatsContainer kitStats) {
        return false;
    }
    
    public static boolean useSkill(final GamePlayer gamePlayer, final KitStatsContainer kitStats) {
        if (gamePlayer.getEnergy() < 25) {
            gamePlayer.sendMessage("§c能量不足！");
            return false;
        }
        final Player player = gamePlayer.getPlayer();
        Player target = null;
        for (final Player player2 : getNearbyPlayers(player.getLocation(), player, 10)) {
            if (target == null || player2.getLocation().distance(player.getLocation()) < target.getLocation().distance(player.getLocation())) {
                target = player2;
            }
        }
        if (target == null) {
            target = player;
        }
        gamePlayer.setEnergy(gamePlayer.getEnergy() - 25);
        player.sendMessage("§a你发出了一道阳炎射线！");
        target.addPotionEffect(new PotionEffect(PotionEffectType.SPEED, 200, 0));
        target.sendMessage("§a" + player.getDisplayName() + "发出了一道阳炎射线！");
        PlayerUtils.heal(target, getHeal(kitStats.getSkillLevel()));
        ParticleEffect.FLAME.display(2.0f, 2.0f, 2.0f, 0.0f, 5, player.getLocation().clone(), 10.0);
        final int i = gamePlayer.getEnergy() / 25;
        gamePlayer.playSound(Sound.ORB_PICKUP, 1.0f, (float)i);
        final List<Location> locations = LocationUtils.getCircle(player.getLocation().clone().add(0.0, -1.0, 0.0), 3.0, 1);
        final Map<Block, Material> blocks = new HashMap<Block, Material>();
        for (final Location location : locations) {
            final Block block = location.getBlock();
            blocks.put(block, block.getType());
            block.setType(Material.STAINED_CLAY);
            block.setData((byte)i);
        }
        Bukkit.getScheduler().runTaskLater((Plugin)MegaWalls.getInstance(), (Runnable)new Runnable() {
            @Override
            public void run() {
                for (final Map.Entry<Block, Material> entry : blocks.entrySet()) {
                    entry.getKey().setType((Material)entry.getValue());
                }
            }
        }, 5L);
        return false;
    }
    
    public static double getHeal(final int level) {
        switch (level) {
            case 1: {
                return 2.2;
            }
            case 2: {
                return 2.4;
            }
            case 3: {
                return 2.6;
            }
            case 4: {
                return 2.8;
            }
            case 5: {
                return 3.0;
            }
            default: {
                return 2.2;
            }
        }
    }
    
    private static List<Player> getNearbyPlayers(final Location location, final Player player, final int radius) {
        final List<Player> players = new ArrayList<Player>();
        final GamePlayer gamePlayer = GamePlayer.get(player.getUniqueId());
        for (final Player other : PlayerUtils.getNearbyPlayers(location, radius)) {
            final GamePlayer gameOther = GamePlayer.get(other.getUniqueId());
            if (!gamePlayer.equals(gameOther) && !gameOther.isSpectator() && gamePlayer.getGameTeam().isInTeam(gameOther)) {
                if (other.getLocation().distance(location) > radius) {
                    continue;
                }
                players.add(other);
            }
        }
        return players;
    }
    
    static {
        set = new HashSet<Material>();
        for (final Material material : Material.values()) {
            MainSkill.set.add(material);
        }
    }
}
